﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdCreatePickModel.h"
#include "IBody.h"
#include "IMesh.h"
#include "IArc3d.h"
#include "IArc2d.h"
#include "IPlane.h"
#include "ILine3d.h"
#include "Matrix4d.h"
#include "IPolygon.h"
#include "ICurve3d.h"
#include "IElement.h"
#include "IDocument.h"
#include "MathUtils.h"
#include "IEllipse3d.h"
#include "IPolyCurve.h"
#include "IModelLine.h"
#include "IUiDocument.h"
#include "IRibbonGroup.h"
#include "IDirectShape.h"
#include "IPolyCurve3d.h"
#include "GmBodyBuilder.h"
#include "GbmpQuickCast.h"
#include "ITorusSurface.h"
#include "IGraphicsStyle.h"
#include "IGraphicsPoint.h"
#include "GbmpFileSystem.h"
#include "IGraphicsPlane.h"
#include "ICommandManager.h"
#include "IAnnotationText.h"
#include "IUserTransaction.h"
#include "GcmpCommandState.h"
#include "IGraphicsCurve3d.h"
#include "IGraphicsBRepBody.h"
#include "IGraphicsBRepFace.h"
#include "IGraphicsMeshBody.h"
#include "IGraphicsStyleData.h"
#include "IGraphicsNodeGroup.h"
#include "UiDocumentViewUtils.h"
#include "AlgorithmDiscretize.h"
#include "IGraphicsPolyCurve3d.h"
#include "IPureGraphicsElement.h"
#include "AlgorithmMeshOperate.h"
#include "IGraphicsElementShape.h"
#include "IGraphicsStyleManager.h"
#include "ICommandButtonDefinition.h"
#include "IControlDefinitionLibrary.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IGraphicsNodeStyleAndMaterialOverride.h"

#include "CommandRegister.h"
#include "SampleUIUtils.h"
#include "Pick/PickFilterDlg.h"
#include "PickSnapCommandIds.h"
#include "ICanvas.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdCreatePickModel)

CmdCreatePickModel::CmdCreatePickModel() : CommandBase(ID_CMD_PICK_CREATE_MODEL){}

bool CmdCreatePickModel::IsEnabled() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

bool CmdCreatePickModel::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

void Transform(IGeometry* pGeometry, int i)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGeometry, L"pGeometry为空",L"GDMP",L"2023-12-30");
    int col = i / 3;
    int row = i % 3;

    Vector3d position = Vector3d(col * 2000, row * 2000, 0);

    Matrix4d matrix;
    matrix.MakeTranslate(position);
    pGeometry->Transform(matrix);
}

/// 将生成的GRep分行分列移动为阵列排布，并且添加注释文字
void TransformAndMark(IDocument* pDocument, IGraphicsElementShape* pGraphicsElementShape, int i, std::wstring comments)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"pDocument为空",L"GDMP",L"2023-12-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGraphicsElementShape, L"pGraphicsElementShape为空",L"GDMP",L"2023-12-30");

    int col = i / 3;
    int row = i % 3;

    std::wstring fontPath = FileSystem::GetExeDirPath() + L"FZLTHJW.TTF";
    Vector3d textPosition(0, -150, 0);
    Vector3d textDirection = Vector3d::UnitX;
    Vector3d textNormal = Vector3d::UnitZ;
    double textHeight = 60;

    AnnotationTextData textData = { comments, fontPath, textPosition, textDirection, textNormal, textHeight, 0, 1 };
    std::vector<OwnerPtr<IGraphicsText>> opGTexts = IAnnotationText::CreateGraphicsTexts(pDocument, textData);
    DBG_WARN_AND_RETURN_VOID_UNLESS(opGTexts.size() > 0, L"opGTexts为空",L"GDMP",L"2023-12-30");
    OwnerPtr<IGraphicsNodeGroup> opGGroupNode = IGraphicsNodeGroup::Create();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opGGroupNode, L"opGGroupNode为空",L"GDMP",L"2023-12-30");
    for (auto &itor : opGTexts)
    {
        opGGroupNode->AddChild(TransferOwnership(itor));
    }
    pGraphicsElementShape->AddChild(TransferOwnership(opGGroupNode));

    Vector3d position = Vector3d(col * 2000, row * 2000, 0);

    Matrix4d matrix;
    matrix.MakeTranslate(position);
    pGraphicsElementShape->Transform(matrix);
}

gcmp::OwnerPtr<gcmp::IAction> CmdCreatePickModel::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocument*  pUiDocument = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocument, L"请打开文档",L"GDMP",L"2023-12-30");
    IDocument* pDocument = pUiDocument->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"请打开文档",L"GDMP",L"2023-12-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDocument, L"创建拾取测试图元");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction, L"opTransaction为空",L"GDMP",L"2023-12-30");
    // index用于计算图元在阵列的位置
    static int index = 0;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");

        // 创建正方形作为放样轮廓
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IPolyCurve3d> opPolyCurve3d = IPolyCurve3d::Create();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opPolyCurve3d, L"opPolyCurve3d为空",L"GDMP",L"2023-12-30");
        Vector3d v1 = Vector3d::Zero;
        Vector3d v2 = Vector3d(1000, 0, 0);
        Vector3d v3 = Vector3d(1000, 500, -500);
        Vector3d v4 = Vector3d(1000, 1000, 0);
        Vector3d v5 = Vector3d(1000, 1000, 1000);

        // 创建放样路径
        OwnerPtr<ILine3d> opLn1 = ILine3d::Create(v1, v2);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opLn1, L"opLn1为空",L"GDMP",L"2023-12-30");
        opPolyCurve3d->AddCurve(TransferOwnershipQuickCast<ICurve3d>(opLn1->Clone()));
        OwnerPtr<IArc3d> opArc = IArc3d::Create(v2, v4, v3);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opArc, L"opArc为空",L"GDMP",L"2023-12-30");
        opPolyCurve3d->AddCurve(TransferOwnershipQuickCast<ICurve3d>(opArc->Clone()));
        OwnerPtr<ILine3d> opLn2 = ILine3d::Create(v4, v5);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opLn2, L"opLn2为空",L"GDMP",L"2023-12-30");
        opPolyCurve3d->AddCurve(TransferOwnershipQuickCast<ICurve3d>(opLn2->Clone()));

        // 创建放样体
        OwnerPtr<IPolygon> opPolygon = IPolygon::Create();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opPolygon, L"opPolygon为空",L"GDMP",L"2023-12-30");
        std::vector<gcmp::Vector2d> points = { Vector2d(-50, -100), Vector2d(50, -100), Vector2d(50, 0), Vector2d(-50, 0), Vector2d(-50, -100) };
        OwnerPtr<IPolyCurve> opPolyCurve = IPolyCurve::Create(points);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opPolyCurve, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        opPolygon->AddPolyCurveByClone(opPolyCurve.get());
        std::vector<const ICurve3d*> opCurves;
        opCurves.push_back(opLn1.get());
        opCurves.push_back(opArc.get());
        opCurves.push_back(opLn2.get());
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateSweepBody(TransferOwnership(opPolygon), opCurves, Vector3d::UnitZ);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsBRepBody> opGraphicsBRepBody = IGraphicsBRepBody::Create(TransferOwnership(opBody));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsBRepBody, L"opGraphicsBRepBody为空",L"GDMP",L"2023-12-30");
        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBRepBody));

        // 在放样体表面创建不可见但是可以拾取的三维线
        OwnerPtr<IGraphicsPolyCurve3d> opGraphicsPolyCurve3d = IGraphicsPolyCurve3d::Create(opPolyCurve3d.get());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsPolyCurve3d, L"opGraphicsPolyCurve3d为空",L"GDMP",L"2023-12-30");
        opGraphicsPolyCurve3d->SetVisibility(GraphicsNodeVisibility::HighlightedExactly);
        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsPolyCurve3d));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"包含HighlightedExactly线的放样体");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        // 创建弧形拉伸体
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateExtrudeBodyByArc(Vector3d::Zero, Vector3d(1000, 0, 0), Vector3d(500, 500, 0), 50, 1000);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsBRepBody> opGraphicsBRepBody = IGraphicsBRepBody::Create(TransferOwnership(opBody));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsBRepBody, L"opGraphicsBRepBody为空",L"GDMP",L"2023-12-30");

        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBRepBody));
        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"弧面体块");

        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        // 创建拉伸体块
        std::vector<Vector3d> points = { Vector3d::Zero, Vector3d(1000, 0, 0), Vector3d(1000, 1000, 0), Vector3d(0, 1000,0) };
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateExtrudeBodyByPoints(points, 1000);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");

        OwnerPtr<IGraphicsBRepBody> opGraphicsBRepBody = IGraphicsBRepBody::Create(TransferOwnership(opBody));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsBRepBody, L"opGraphicsBRepBody为空",L"GDMP",L"2023-12-30");

        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBRepBody));
        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"透明的体块");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));

        // 将拉伸体块的图形样式设置为全透明
        const IGraphicsStyleManager *pGraphicsStyleManager = pDocument->GetGraphicsStyleManager();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGraphicsStyleManager, L"pGraphicsStyleManager为空",L"GDMP",L"2023-12-30");
        ElementId styleId = pGraphicsStyleManager->GetGraphicsStyleIdByCategoryUid(BuiltInCategoryUniIdentities::BICU_FORM);
        IGraphicsStyle* pFormGraphicsStyle = quick_cast<IGraphicsStyle>(pDocument->GetElement(styleId));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFormGraphicsStyle, L"pFormGraphicsStyle为空",L"GDMP",L"2023-12-30");

        IGraphicsStyle* pGraphicsStyle = IGraphicsStyle::Create(pDocument, ElementCreationOptions::Normal);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGraphicsStyle, L"pGraphicsStyle为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsStyleData> opGraphicsStyleData = pFormGraphicsStyle->GetGraphicsStyleData();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsStyleData, L"opGraphicsStyleData为空",L"GDMP",L"2023-12-30");
        opGraphicsStyleData->SetTransparency(0);
        pGraphicsStyle->SetGraphicsStyleData(*opGraphicsStyleData.get());

        IElement* pElement = pDirectShape->GetOwnerElement();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"pElement为空",L"GDMP",L"2023-12-30");
        pGraphicsStyle->SetOwnerId(pElement->GetElementId());

        IGraphicsNodeStyleAndMaterialOverride* pGraphicsNodeStyleAndMaterialOverride = pElement->GetShapeGraphicsNodeStyleAndMaterialOverrideComponentFW();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGraphicsNodeStyleAndMaterialOverride, L"pGraphicsNodeStyleAndMaterialOverride为空",L"GDMP",L"2023-12-30");

        // 项目图元级设置图元显示样式
        pGraphicsNodeStyleAndMaterialOverride->OverrideGraphicsStyle(pGraphicsStyle->GetElementId());
    }

    index++;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_TEMP_STYLE);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        // 创建点阵
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                OwnerPtr<IGraphicsPoint> opGraphicsPoint = IGraphicsPoint::Create({ i * 100.0, j * 100.0, 500 });
                DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsPoint, L"opGraphicsPoint为空",L"GDMP",L"2023-12-30");
                opGraphicsPoint->SetDisplayShape(PointDisplayShapeType::Circle);
                opGraphicsPoint->SetId(GraphicsNodeId(i*10 +j));
                opGraphicsPoint->SetDepthNormalMode(true);
                opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsPoint));
            }
        }

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"IGraphicsPoint组成的图元");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        std::vector<Vector3d> points = { Vector3d::Zero, Vector3d(1000, 0, 0), Vector3d(1000, 1000, 0), Vector3d(0, 1000,0) };
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateExtrudeBodyByPoints(points, 1000);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsBRepBody> opGraphicsBRepBody = IGraphicsBRepBody::Create(TransferOwnership(opBody));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsBRepBody, L"opGraphicsBRepBody为空",L"GDMP",L"2023-12-30");
        // 将体块的面都设置为不可拾取
        for (int i = 0; i < opGraphicsBRepBody->GetFaceCount(); i++)
        {
            IGraphicsBRepFace* pBrepFace = opGraphicsBRepBody->GetFaceFW(i);
            DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pBrepFace, L"pBrepFace为空",L"GDMP",L"2023-12-30");
            pBrepFace->SetIsSelectable(false);
        }
        // 目前拾取不支持IGraphicsBRepBody，因此下面设置没有任何效果，也不影响其他图形节点的拾取
        opGraphicsBRepBody->SetIsSelectable(false);
        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBRepBody));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"IGraphicsBRepFace::SetIsSelectable为False，体块的面不可拾取");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        // 创建圆环面
        gcmp::Coordinate3d coord(Vector3d(600, 600, 0), Vector3d::UnitX, Vector3d::UnitY);
        gcmp::OwnerPtr<gcmp::IArc2d> opArc2d = gcmp::IArc2d::Create({}, 200, 0, gcmp::Constants::MATH_2PI);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opArc2d, L"opArc2d为空",L"GDMP",L"2023-12-30");
        gcmp::OwnerPtr<gcmp::ITorusSurface> opTorus = gcmp::ITorusSurface::Create(coord, gcmp::Intervald(0, gcmp::Constants::MATH_2PI* 500), opArc2d.get(), 500, false);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTorus, L"opTorus为空",L"GDMP",L"2023-12-30");

        // 将圆环面转化为mesh面
        std::vector<gcmp::Vector3d> vertexList, normalList;
        std::vector<int> triIndexs;
        std::vector<gcmp::Vector2d> vertexUVs;
        bool success = AlgorithmDiscretize::Discretize(opTorus.get(), 0.1, 0.314, vertexList, normalList, triIndexs, &vertexUVs);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(success, L"Discretize失败",L"GDMP",L"2023-12-30");
        OwnerPtr<IMesh> opMesh = IMesh::Create(vertexList, normalList, vertexUVs, triIndexs, triIndexs, triIndexs);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opMesh, L"opMesh为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsMeshBody> opGrepMeshBody = IGraphicsMeshBody::Create(TransferOwnership(opMesh));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGrepMeshBody, L"opGrepMeshBody为空",L"GDMP",L"2023-12-30");
        opGrepMeshBody->SetIsSnappable(true);
        opGraphicsElementShape->AddChild(TransferOwnership(opGrepMeshBody));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"Mesh面");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        // 创建圆弧
        gcmp::OwnerPtr<gcmp::IArc3d> opArc = gcmp::IArc3d::Create(Vector3d(0, 1000, 0), Vector3d(1000, 0, 0), Vector3d(800, 800, 0));

        Transform(opArc.get(), index);
        IModelLine* pModelLine = IModelLine::Create(pDocument, TransferOwnership(opArc));
    }

    index++;
    {
        // 创建椭圆
        gcmp::OwnerPtr<gcmp::IEllipse3d> opEllipse3d = IEllipse3d::Create(Vector3d(500, 500, 0), Vector3d::UnitZ, Vector3d::UnitX, 500, 300);
        Transform(opEllipse3d.get(), index);
        IModelLine* pModelLine = IModelLine::Create(pDocument, TransferOwnership(opEllipse3d));
    }

    index++;
    {
        // 创建空间体块
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        std::vector<Vector3d> points = { Vector3d::Zero, Vector3d(1000, 0, 0), Vector3d(1000, 1000, 0), Vector3d(0, 1000,0) };
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateExtrudeBodyByPoints(points, 1000);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsBRepBody> opGraphicsBRepBody = IGraphicsBRepBody::Create(TransferOwnership(opBody));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsBRepBody, L"opGraphicsBRepBody为空",L"GDMP",L"2023-12-30");

        Matrix4d matrix = Matrix4d::CreateIdentityMatrix4d();
        bool rotateResult = matrix.MakeRotate(Vector3d(500, 500, 500), Vector3d(1, 1, 1), MathUtils::AngleToRadians(45));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(rotateResult, L"旋转失败",L"GDMP",L"2023-12-30");
        opGraphicsBRepBody->Transform(matrix);

        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBRepBody));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"空间体块");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        // IDirectShape中创建两根线，一根可以捕捉，一根不可以捕捉
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        OwnerPtr<ILine3d> opLine1 = ILine3d::Create(Vector3d(0, 0, 0), Vector3d(1000, 0, 0));
        OwnerPtr<ILine3d> opLine2 = ILine3d::Create(Vector3d(0, 500, 0), Vector3d(1000, 500, 0));
        OwnerPtr<IGraphicsCurve3d> opGCurve1 = IGraphicsCurve3d::Create(opLine1.get());
        OwnerPtr<IGraphicsCurve3d> opGCurve2 = IGraphicsCurve3d::Create(opLine2.get());
        opGCurve1->SetIsSnappable(true);
        opGCurve2->SetIsSnappable(false);

        opGraphicsElementShape->AddChild(TransferOwnership(opGCurve1));
        opGraphicsElementShape->AddChild(TransferOwnership(opGCurve2));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"上面的线不能捕捉，下面的线可以捕捉");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        // 创建一个平面
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        OwnerPtr<IGraphicsPlane> opGPlane = IGraphicsPlane::CreateByNormal(Vector3d::Zero, Vector3d::UnitZ);
        opGPlane->SetLimits(Intervald(0, 1000), Intervald(0, 1000));
        opGPlane->SetIsSnappable(true);

        opGraphicsElementShape->AddChild(TransferOwnership(opGPlane));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"平面IPlane");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    {
        // 创建空间mesh体块，中心有一自定义捕捉点
        IDirectShape* pDirectShape = IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2023-12-30");

        std::vector<Vector3d> points = { Vector3d::Zero, Vector3d(1000, 0, 0), Vector3d(1000, 1000, 0), Vector3d(0, 1000,0) };
        OwnerPtr<IBody> opBody = GmBodyBuilder::CreateExtrudeBodyByPoints(points, 1000);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBody, L"opBody为空",L"GDMP",L"2023-12-30");

        OwnerPtr<IMesh> opMesh;
        AlgorithmMeshOperate::CreateMeshFromBrepBody(opBody.get(), 10, 10, 10, DiscreteEpsilonType::Common, opMesh);
        OwnerPtr<IGraphicsMeshBody> opGraphicsMeshBody = IGraphicsMeshBody::Create(TransferOwnership(opMesh));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsMeshBody, L"opGraphicsMeshBody为空",L"GDMP",L"2023-12-30");

        OwnerPtr<IGraphicsPoint> opGCenterPoint = IGraphicsPoint::Create(Vector3d(500, 500, 500));
        opGCenterPoint->SetDepthNormalMode(true);

        opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsMeshBody));
        opGraphicsElementShape->AddChild(TransferOwnership(opGCenterPoint));

        TransformAndMark(pDocument, opGraphicsElementShape.get(), index, L"Mesh空间体块，中心有自定义捕捉点");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    index++;
    opTransaction->Commit();

    // 需要刷新视图后，然后缩放视图到现有模型才起效果
    IUiView* pCurrentView = UiDocumentViewUtils::GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMP",L"2023-12-30");
    pCurrentView->GetCanvas()->Refresh();

    ICommandManager* pCommandManager = ICommandManager::Get();
    pCommandManager->SendCommand(L"gmZoomFitCmd");

    return nullptr;
}

